<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;


/**
 * App\Models\Shop
 *
 * @property int $shop_id 店铺ID
 * @property string $shop_name 店铺名称
 * @property int $uid 店主UID
 * @property string $username 店主姓名
 * @property string $logo 店铺标志
 * @property int $type 店铺类型，1=个人店铺，2=企业店铺
 * @property string $phone 手机号码
 * @property string $province 所在省
 * @property string $city 所在市
 * @property string $district 所在县
 * @property string $street 街道
 * @property string $created_at 开店时间
 * @property string $updated_at 更新时间
 * @property int $views 浏览次数
 * @property int $collections 收藏数量
 * @property int $subscribes 关注量
 * @property int $orders 订单数
 * @property int $visitors 访客数
 * @property float $turnover 营业额
 * @property int $item_count 商品数
 * @property int $total_sold 累计销量
 * @property float $lat 纬度
 * @property float $lng 经度
 * @property string $description 店铺简介
 * @property int $main_source 主要货源
 * @property int $closed 已关闭
 * @property int $auth_status 认证状态
 * @property int $bond_status 缴纳保证金状态
 * @property-read \App\Models\ShopAuth $auth
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShopEvaluate[] $evaluates
 * @property-read \App\Models\ShopContent $content
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Item[] $items
 * @property-read \App\Models\User $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop closed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop opening()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop popular()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereAuthStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereBondStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereClosed($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereCollections($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereDescription($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereDistrict($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereItemCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereLat($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereLng($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereLogo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereMainSource($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereOrders($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereProvince($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereShopId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereShopName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereStreet($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereSubscribes($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereTotalSold($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereTurnover($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereUsername($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereViews($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereVisitors($value)
 * @mixin \Eloquent
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShopKefu[] $kefus
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShopModelName[] $itemModels
 */
class Shop extends Model
{
    protected $table = 'shop';
    protected $primaryKey = 'shop_id';
    protected $guarded = ['shop_id'];

    public $timestamps = false;

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        static::creating(function ($model){
            $model->created_at = time();
            $model->updated_at = time();
        });

        static::updating(function ($model){
            $model->updated_at = time();
        });

        static::created(function (Shop $shop){
            $shop->content()->create();
            $shop->auth()->create();
        });

        static::deleted(function (Shop $shop){
            $shop->content()->delete();
            $shop->auth()->delete();
            $shop->evaluates()->delete();
            $shop->items()->delete();
            $shop->kefus()->delete();
        });
    }

    /**
     * 营业中的店铺
     */
    public function scopeOpening($query)
    {
        return $query->where('closed', 0);
    }

    /**
     * 已关闭的店铺
     * @return Shop
     */
    public function scopeClosed($query)
    {
        return $query->where('closed', 0);
    }

    /**
     * 按销量排序
     * @param $query
     * @return mixed
     */
    public function scopePopular($query)
    {
        return $query->orderByDesc('total_sold');
    }

    public function incrementTotalSold($num) {
        return $this->increment('totalSold', $num);
    }

    public function incrementViews($num = 1)
    {
        return $this->increment('views', $num);
    }

    public function incrementItems($num = 1)
    {
        return $this->increment('items', $num);
    }

    public function incrementOrders($num = 1)
    {
        return $this->increment('orders', $num);
    }

    /**
     * @param $amount
     * @return mixed
     */
    public function incrementTurnover($amount)
    {
        return $this->increment('turnover', $amount);
    }

    public function incrementVisitors($num = 1)
    {
        return $this->increment('visitors', $num);
    }

    public function incrementCollections($num = 1)
    {
        return $this->increment('collections', $num);
    }

    public function incrementSubscribes($num)
    {
        return $this->increment('subscribes', $num);
    }

    public function items()
    {
        return $this->hasMany(Item::class, 'shop_id', 'shop_id');
    }

    public function auth()
    {
        return $this->hasOne(ShopAuth::class, 'shop_id', 'shop_id');
    }

    public function content()
    {
        return $this->hasOne(ShopContent::class, 'shop_id', 'shop_id');
    }

    public function kefus()
    {
        return $this->hasMany(ShopKefu::class, 'shop_id', 'shop_id');
    }

    public function isOpening()
    {
        return !$this->closed;
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'uid', 'uid');
    }

    public function evaluates()
    {
        return $this->hasMany(ShopEvaluate::class, 'shop_id', 'shop_id');
    }

    public function itemModels()
    {
        return $this->hasMany(ShopModelName::class, 'shop_id', 'shop_id');
    }
}
